package com.moyun.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
public class QuartzUtil {

    /**
     * 注入Scheduler
     */
    @Autowired
    private Scheduler scheduler;

    /**
     * 创建CronTrigger
     * @param triggerName  触发器名
     * @param triggerGroupName 触发器组名
     * @param cronExpression  定时任务表达式
     */
    public CronTrigger createCronTrigger(String triggerName,String triggerGroupName ,String cronExpression){
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName)
                .withSchedule(cronScheduleBuilder).build();
        return cronTrigger;
    }

    /**
     * 创建JobDetail
     * @param jobDetailName 任务名称
     * @param jobClass  任务类，实现Job类接口
     */
    public JobDetail createCronScheduleJob(String jobDetailName,Class<? extends Job> jobClass){
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobDetailName).build();
        return jobDetail;
    }


    /**
     * 修改cron任务的执行间隔
     * @param triggerName   旧触发器名
     * @param triggerGroupName 旧触发器组名
     * @param newCronTime
     * @throws SchedulerException
     */
    public boolean updateCronScheduleJob(String triggerName,String triggerGroupName,String newCronTime) throws SchedulerException {
        Date date;
        log.info("updateCronScheduleJob 入参name={}，triggerGroupName={}，newCronTime={}",triggerName,triggerGroupName,newCronTime);
        TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if(ObjectUtils.isEmpty(cronTrigger)){
            log.error("获取到的cronTrigger为null！");
            return false;
        }
        String oldTime = cronTrigger.getCronExpression();
        log.info("oldTimeCron={},newCronTime={}",oldTime,newCronTime);
        if (!oldTime.equalsIgnoreCase(newCronTime)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(newCronTime);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
            log.info("修改执行成功，下次任务开始time={}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            return true;
        }else{
            log.error("oldTimeCron与newCronTime相等，修改结束");
            return false;
        }
    }


    /**
     * 修改time任务的执行间隔
     * @param triggerName   旧触发器名
     * @param triggerGroupName 旧触发器组名
     * @param newCronTime
     * @throws SchedulerException
     */
    public boolean updateTimeScheduleJob(String triggerName,String triggerGroupName,String newCronTime) throws SchedulerException {
        Date date;
        log.info("updateTimeScheduleJob 入参name={}，triggerGroupName={}，newCronTime={}",triggerName,triggerGroupName,newCronTime);
        TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if(ObjectUtils.isEmpty(cronTrigger)){
            log.error("获取到的cronTrigger为null！");
            return false;
        }
        String oldTime = cronTrigger.getCronExpression();
        log.info("oldTimeCron={},newCronTime={}",oldTime,newCronTime);
        if (!oldTime.equalsIgnoreCase(newCronTime)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(newCronTime);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
            log.info("修改执行成功，下次任务开始time={}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            return true;
        }else{
            log.error("oldTimeCron与newCronTime相等，修改结束");
            return false;
        }
    }

}

